package com.chen.gulimall.coupon.service.impl;

import com.chen.gulimall.coupon.entity.MemberPriceEntity;
import com.chen.gulimall.coupon.entity.SkuLadderEntity;
import com.chen.gulimall.coupon.service.MemberPriceService;
import com.chen.gulimall.coupon.service.SkuLadderService;
import com.chen.gulimallcommon.common.to.FullReduceTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.gulimallcommon.common.utils.PageUtils;
import com.chen.gulimallcommon.common.utils.Query;
import com.chen.gulimall.coupon.dao.SkuFullReductionDao;
import com.chen.gulimall.coupon.entity.SkuFullReductionEntity;
import com.chen.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveFullReduce(FullReduceTo fullReduceTo) {
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setId(fullReduceTo.getSkuId());
        skuLadderEntity.setFullCount(fullReduceTo.getFullCount());
        skuLadderEntity.setDiscount(fullReduceTo.getDiscount());
        skuLadderEntity.setAddOther(fullReduceTo.getCountStatus());
        if (skuLadderEntity.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }


        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(fullReduceTo,skuFullReductionEntity);
        skuFullReductionEntity.setSkuId(fullReduceTo.getSkuId());
        skuFullReductionEntity.setAddOther(fullReduceTo.getCountStatus());
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            this.save(skuFullReductionEntity);
        }


        List<MemberPriceEntity> list = fullReduceTo.getMemberPrice().stream().map(item-> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(fullReduceTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(memberPriceEntity -> {
            return memberPriceEntity.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
        }).collect(Collectors.toList());

        memberPriceService.saveBatch(list);

    }

}